﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Framework.DataAccess;

public partial class PurchaseAudit : PMSBase
{ 
    private SqlCodeConfig codeConfig;
    string cmdText = "SELECT T.*,A.CodeName AS PurchaseTypeName" + PMSAccess.AppendSql_UserOrg + "  FROM {0} T  LEFT JOIN " + SqlCodeConfig.TableName + " A ON A.CodeValue=T.PurchaseType AND A.Category='" + PMSCodeCategory.PMS_CGLX + "' where 1=1 ";
    protected void Page_Load(object sender, EventArgs e)
    { 
        this.TableName = "IC_PurchaseOrder";
        this.Key = "PurchaseOrderID";
        if (!IsPostBack)
        {
            pnlGrid.Visible = true;
            pnlEdit.Visible = false;
            string sql = string.Format(cmdText+" ORDER BY T.STATUS ASC,CREATETIME DESC", this.TableName);
            FillGirdView(0, sql);
            InitLoadData();
            if (!string.IsNullOrEmpty(Request["PurchaseOrderID"]))
            {
                btnView_Click(null, e);
            }
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
        GoodItems.IsShowPrice = false;
    }
    private void InitLoadData()
    {  
        codeConfig = new SqlCodeConfig(this.Connection);
        DataSet ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_CGLX);
        this.BindDropDownList(S_PurchaseType, ds, true); 
        this.BindDropDownList(P_PurchaseType, ds, true);
        ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_CGSOURCE);
        this.BindDropDownList(P_PurchaseSource, ds, true);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        StringBuilder sql = new StringBuilder(string.Format(cmdText, this.TableName));
        if (!string.IsNullOrEmpty(S_PurchaseOrderCode.Text))
        {
            sql.Append(" AND T.PurchaseOrderCode like '%").Append(S_PurchaseOrderCode.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_PurchaseDate.Text))
        {
            sql.Append(" AND T.PurchaseDate = '").Append(S_PurchaseDate.Text).Append("'");
        }
        if (!string.IsNullOrEmpty(S_PurchaseType.SelectedValue))
        {
            sql.Append(" AND T.PurchaseType = '").Append(S_PurchaseType.SelectedValue).Append("'");
        }
        sql.Append(" ORDER BY T.STATUS ASC,T.CREATETIME DESC");
        FillGirdView(0, sql.ToString());
    }
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    } 
    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }
    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((HyperLink)e.Row.Cells[1].Controls[0]).NavigateUrl = "PurchaseAudit.aspx?PurchaseOrderID=" + ((HiddenField)e.Row.Cells[0].Controls[1]).Value;
            e.Row.Cells[2].Text = this.ParseDateToShort(e.Row.Cells[2].Text);
            e.Row.Cells[7].Text = OrderStatus.GetName(e.Row.Cells[7].Text);
        } 
    }
    private void ResetComponent()
    {
        this.ClearPrexControlValues(this.DefaultPrex); 
    }  
    protected void btnView_Click(object sender, EventArgs e)
    {
        string poid = Request.Form["PurchaseOrderID"];
        if(sender==null)
            poid = Request["PurchaseOrderID"];
        if (string.IsNullOrEmpty(poid))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        ResetComponent();
        hfAddOrEdit.Value = "View";
        hfPurchaseOrderID.Value = poid;

        string sql = string.Format(cmdText + " AND T.PurchaseOrderID='" + poid + "'", this.TableName);
        DataSet ds = PMSAccess.GetDataSet(this.Connection, sql, this.TableName);
        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
            Alert("您选择的记录已经被删除，请刷新数据！");
            return;
        }
        DataRow dr = ds.Tables[this.TableName].Rows[0]; 
        pnlGrid.Visible = false;
        pnlEdit.Visible = true; 
        SetReadOnly(this.DefaultPrex, true); 
        SessionManager.DataSet = ds;
        this.FillValueByPrexFromDataRow(dr, this.DefaultPrex);
        P_LoginName.Text = dr["UserName"].ToString();
        P_Department.Text = dr["OrgName"].ToString(); 
        GoodItems.IsReadOnly = true;
        GoodItems.GoodCountCaption = "采购数量";
        GoodItems.InitData("IC_PurchaseOrderItem", "PurchaseOrderItemID", "PurchaseOrderID", poid);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlGrid.Visible = true;
        pnlEdit.Visible = false;
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    { 
        string sql = "select * from IC_PurchaseOrder where PurchaseOrderID='" + hfPurchaseOrderID.Value + "'";
        DataSet ds = PMSAccess.GetDataSet(this.Connection, sql, "IC_PurchaseOrder");
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataRow dr = ds.Tables[0].Rows[0];
            int status = Convert.ToInt32(ds.Tables[0].Rows[0]["Status"]);
            if (OrderStatus.IsAudited == status)
            {
                base.Alert("您选择的记录已经审核过");
                return;
            }
            else if (OrderStatus.IsStockIn == status)
            {
                base.Alert("您选择的记录已经入库");
                return;
            }
            else
            {
                dr["Status"] = OrderStatus.IsAudited;
                PMSAccess.UpdateDataSet(this.Connection, "IC_PurchaseOrder", ds);
                pnlGrid.Visible = true;
                pnlEdit.Visible = false;
                FillGirdView(this.gridView.SplitPageIndex, string.Empty);
                tipMessage1.ShowMessage("审核成功", 5000);
            }
        }
        else
        {
            base.Alert("您选择的记录不存在");
            return;
        } 
    } 
    protected void btnAudit_Click(object sender, EventArgs e)
    {
        string poid = Request.Form["PurchaseOrderID"];
        if (string.IsNullOrEmpty(poid))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        string sql = "select * from IC_PurchaseOrder where PurchaseOrderID='" + poid + "'";
        DataSet ds = PMSAccess.GetDataSet(this.Connection, sql, "IC_PurchaseOrder");
        if(ds.Tables[0].Rows.Count>0)
        {
            DataRow dr = ds.Tables[0].Rows[0];
            int status = Convert.ToInt32(ds.Tables[0].Rows[0]["Status"]);
            if (OrderStatus.IsAudited == status)
            {
                base.Alert("您选择的记录已经审核过");
                return;
            }
            else if (OrderStatus.IsStockIn == status)
            {
                base.Alert("您选择的记录已经入库");
                return;
            }
            else
            {
                dr["Status"] = OrderStatus.IsAudited;
                PMSAccess.UpdateDataSet(this.Connection, "IC_PurchaseOrder", ds);
                pnlGrid.Visible = true;
                pnlEdit.Visible = false;
                FillGirdView(this.gridView.SplitPageIndex, string.Empty);
                tipMessage1.ShowMessage("审核成功", 5000);
            }
        }
        else
        {
            base.Alert("您选择的记录不存在");
            return; 
        } 
    } 
}
